import replace from 'lodash/replace';
import { of } from 'rxjs';
import { zip } from 'rxjs/operators';

/**
 * 字符串解密
 * @param st
 */
const encodeStr = (st: string): string => {
  const a = replace(replace(replace(st, /\&/g, '8'), /\*/g, 'c'), /\%/g, 'a');
  const b = a
    .split('')
    .reverse()
    .join('');

  return b;
};

/**
 * 拼接字符串
 * @param a
 * @param b
 * @param c
 * @param f
 */
const pieceStr = (a: string, b: string, c: string): string => {
  let str: string = '';

  of(a)
    .pipe(zip(of(b), of(c)))
    .subscribe(values => {
      str = values.reverse().join('');
    });
  return str;
};

/**
 * 接口签名算法加密
 * @param params 这个参数只是为了动态化参数，防止被tree-shaking给压缩了
 */
export const getSecret = (params = {}) => {
  const sA = process.env.appSecuritA as string;
  const sB = process.env.appSecuritB as string;
  const sC = process.env.appSecuritC as string;
  (window as any)._serviceTimer = Date.now();
  return {
    ...params,
    data: pieceStr(encodeStr(sA), encodeStr(sB), encodeStr(sC))
  };
};

/**
 * 拼接AppKey 签名参数
 * @param params 这个参数只是为了动态化参数，防止被tree-shaking给压缩了
 */
export const getAppKey = (params = {}): any => {
  const kA = process.env.appKeyA as string;
  const kB = process.env.appKeyB as string;
  const kC = process.env.appKeyC as string;
  return {
    ...params,
    data: pieceStr(encodeStr(kA), encodeStr(kB), encodeStr(kC))
  };
};

/**
 * 拼接AppSite 站点映射参数
 * @param params 这个参数只是为了动态化参数，防止被tree-shaking给压缩了
 */
export const getAppSite = (params = {}): any => {
  const kA = process.env.appSiteA as string;
  const kB = process.env.appSiteB as string;
  const kC = process.env.appSiteC as string;
  return {
    ...params,
    data: pieceStr(encodeStr(kA), encodeStr(kB), encodeStr(kC)),
  };
};